package com.travelexp;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.LocalBroadcastManager;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class MainMenu extends Activity 
{
	public String curTravel;
	private TravelData travelData;
	private static Context myContext;
	private Uri imageUri;
	private String imageLocation;
	private static final int CAMERA_PIC_REQUEST = 1337;
	private LocalActivityManager activityGroup; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_menu);
		myContext = getApplicationContext();
		activityGroup = new LocalActivityManager(this, true);
		// Creates the Travel Date
		travelData = new TravelData(this);

		// Displays the current travel
		displayCurTravel();

		// Setup the tabs
		setupTabs(savedInstanceState);

		// Create Service
		Intent serviceIntent = new Intent(this, UpdaterService.class);
		startService(serviceIntent);	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Menu
		getMenuInflater().inflate(R.menu.activity_main_menu, menu);

		return true;
	}

	@Override
	protected void onResume() {
		// Displays the current travel
		displayCurTravel();
		super.onResume();
	}

	@Override
	protected void onRestart() {
		displayCurTravel();
		super.onRestart();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_new_travel:
			return newTravel();
		case R.id.menu_settings:
			return preferences();
		case R.id.menu_arc_travels:
			return arcTravel();
		case R.id.menu_camera:
			return camera();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	// Return the application context
	public static Context getAppContext() {
		return myContext;
	}

	// Display the current travel
	public void displayCurTravel() {
		TextView travelName = (TextView) findViewById(R.id.travel_name);
		String curTravel = travelData.getCurTravel();
		if (curTravel == null) {
			travelName.setText(R.string.empty_travel);
		} else {
			travelName.setText(curTravel);
		}
		// Update the Locals list
		Intent intent = new Intent(LocalsList.UPDATE_LOCALS);
		LocalBroadcastManager.getInstance(this).sendBroadcast(intent);				
	}

	// Call New_Travel Screen
	public boolean newTravel() {
		Intent intent = new Intent(this, TravelForm.class);
		startActivity(intent);
		return true;
	}

	// Open the activity archived travels
	public boolean arcTravel() {
		Intent intent = new Intent(this, OpenTravel.class);
		startActivity(intent);
		return true;
	}

	// Call preferences Screen
	public boolean preferences() {
		Intent intent = new Intent(this, Prefs.class);
		startActivity(intent);
		return true;
	}

	// Call Camera
	public boolean camera() {
		Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		File photo = new File(Environment.getExternalStorageDirectory(), "pic.jpg");
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
		imageUri = Uri.fromFile(photo);
		startActivityForResult(intent, CAMERA_PIC_REQUEST);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == CAMERA_PIC_REQUEST) {
			travelData.insertImage(imageUri.getPath());
		}
	}

	// Setup the tabs
	public void setupTabs(Bundle state) {
		// We are using the deprecated class because otherwise we would have to 
		// create a new FragmentManager class. This would take too much time
		// for this simple task
		TabHost th = (TabHost) findViewById(R.id.tab_host);
	    activityGroup.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
	    th.setup(activityGroup);
	    
		// Locals Tab
		TabSpec specs = th.newTabSpec("tabLocals");
		Intent intent = new Intent();
		intent.setClass(this, LocalsList.class);
		specs.setIndicator(getString(R.string.locals));		
		specs.setContent(intent);
		th.addTab(specs);

		// Image Tab
		specs = th.newTabSpec("tabImages");
		intent = new Intent();
		intent.setClass(this, PhotosActivity.class);
		specs.setIndicator(getString(R.string.images));		
		specs.setContent(intent);
		th.addTab(specs);

		// Others Tabs
		specs = th.newTabSpec("tabRoute");
		intent = new Intent();
		intent.setClass(this, MapViewer.class);
		specs.setContent(intent);
		specs.setIndicator(getString(R.string.route));
		th.addTab(specs);
	}
}

